package Services;

import Entity.Vaccine;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

public class VaccineService implements Serializable {

    public VaccineService(EntityManagerFactory emf) {
        this.emf = emf;  
    }
    private EntityManagerFactory emf = null;
  
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Vaccine vaccine) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(vaccine);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Vaccine vaccine) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            vaccine = em.merge(vaccine);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = vaccine.getId();
                if (findVaccine(id) == null) {
                    throw new NonexistentEntityException("The vaccine whit id" + id + "no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vaccine vaccine;
            try {
                vaccine = em.getReference(Vaccine.class, id);
                vaccine.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The vaccine whit id" + id + "no longer exists.", enfe);
            }
            em.remove(vaccine);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Vaccine> findVaccineEntities() {
        return findVaccineEntities(true, -1, -1);
    }

    public List<Vaccine> findVaccineEntities(int maxResults, int firstResult) {
        return findVaccineEntities(false, maxResults, firstResult);
    }

    private List<Vaccine> findVaccineEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Vaccine.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Vaccine findVaccine(Long id) {
        EntityManager em = getEntityManager();
        try {
            Vaccine vaccine = em.find(Vaccine.class, id);
            return vaccine;
        } finally {
            em.close();
        }
    }
}
